//精灵的基类，负责初始化精灵加载的资源和大小以及位置
import {DataStore} from "./DataStore.js";

export class Sprite {
    constructor(img = null,
                srcX = 0,
                srcY = 0,
                srcW = 0,
                srcH = 0,
                x = 0, y = 0,
                width = 0,
                height = 0) {
        this.dataStore = DataStore.getInstance();
        this.ctx = this.dataStore.ctx;
        this.img = img;
        // 需要裁剪的位置
        this.srcX = srcX;
        this.srcY = srcY;
        // 需要裁剪的尺寸
        this.srcW = srcW;
        this.srcH = srcH;
        // 绘制的位置
        this.x = x;
        this.y = y;
        // 绘制的大小
        this.width = width;
        this.height = height;
    }

    static getImage(key) {
        return DataStore.getInstance().res.get(key);
    }

    draw(
        img = this.img,
        srcX = this.srcX,
        srcY = this.srcY,
        srcW = this.srcW,
        srcH = this.srcH,
        x = this.x,
        y = this.y,
        width = this.width,
        height = this.height
    ) {
        this.ctx.drawImage(img, srcX, srcY, srcW, srcH, x, y, width, height);
    }
}